package com.jt.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jt.pojo.Item;
import com.jt.vo.EasyUITable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jt.mapper.ItemMapper;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service
public class ItemServiceImpl implements ItemService {
	
	@Autowired
	private ItemMapper itemMapper;


	@Override
	public EasyUITable findItemByPage(Integer page, Integer rows) {
		/*long total=itemMapper.selectCount(null);
		//手写分页
		int startIndex=(page-1)*rows;
       List<Item> itemList=itemMapper.findItemByPage( startIndex, rows);*/

		IPage mpPage=new Page(page,rows);
		QueryWrapper<Item> queryWrapper=new QueryWrapper<>();
		queryWrapper.orderByDesc("updated");
		mpPage = itemMapper.selectPage(mpPage, queryWrapper);
		long total=mpPage.getTotal();//获取总记录数
		List<Item> itemList=mpPage.getRecords();//获取查询当前页
		return new EasyUITable(total,itemList);
	}

	@Override
	@Transactional  //开启事务控制
	public void saveItem(Item item) {
		/*Date date=new Date();
		item.setStatus(1).setCreated(date).setUpdated(date);*/
		item.setStatus(1);
		itemMapper.insert(item);
       // int i=1/0;
	}

	@Override
	public void ipdateItem(Item item) {
		itemMapper.updateById(item);
	}

	@Override
	public void deleteItem(Long[] ids) {
		UpdateWrapper updateWrapper=new UpdateWrapper();
		updateWrapper.in("id", ids);
		itemMapper.delete(updateWrapper);

		//List<Long> longList= Arrays.asList(ids);
		//itemMapper.deleteBatchIds(longList);


		//itemMapper.deleteItem(ids);
	}
}
